package imagetools;

import com.baidu.aip.util.Base64Util;
import org.json.JSONObject;

import java.io.File;
import java.io.FileOutputStream;
import java.io.OutputStream;

public class Main {

    public static void main(String[] args) throws Exception {
        String inputPath = "E:\\eclipse-workspace\\javasdk\\image\\input";
        String outputPath = "E:\\eclipse-workspace\\javasdk\\image\\output";
        int action=0;
        if (args != null && args.length >= 3) {
            action =Integer.parseInt(args[0]);
            inputPath = args[1];
            outputPath = args[2];
        }else{
            System.err.println("参数错误！至少满足3个参数。");
        }
        System.out.println("图片处理开始");
        File inputPathDir = new File(inputPath);
        JSONObject jsonObject = null;
        String output_json = null;
        String log="";
        if (inputPathDir.isDirectory()) {
            int count=inputPathDir.listFiles().length;
            int index=0;
            for (File file : inputPathDir.listFiles()) {
                System.out.println("");
                System.out.println("["+(1+index++)+"/"+count+"]\t"+file.getAbsolutePath());
                log+="\n"+file.getName();
                log+="\n"+"####"+ ImgCensor.scenes(file.getAbsolutePath());
//                Map map = ImageUtil.getImageInfoByFile(file.getAbsolutePath());
//                int width = (Integer) map.get("width");
//                int height = (Integer) map.get("height");
                //图片太大不进行放大处理
               // if (width > 2000 || height > 1500) {
                //    FileUtil.copyFile(file.getAbsolutePath(), outputPath);
               //     continue;
                //}else{
                    //图片无损放大
                if(action == 0)
                    output_json = ImageQualityEnhance.imageQualityEnhance(file.getAbsolutePath());
              //  }
                //图片去雾
//                if(output_json!=null){
//                    jsonObject = new JSONObject(output_json);
//                    output_json = Dehaze.dehazeByBase64((String) jsonObject.get("image"));
//                }else{
//                    output_json = Dehaze.dehaze(file.getAbsolutePath());
//                }
                if(action == 1){
                    output_json = Dehaze.dehaze(file.getAbsolutePath());
                }
                //

                //图片增加对比度
                if(action == 2){
                    output_json = ContrastEnhance.contrastEnhance(file.getAbsolutePath());
                }
//                if(output_json!=null){
//                    jsonObject = new JSONObject(output_json);
//                    output_json = ContrastEnhance.contrastEnhanceByBase64((String) jsonObject.get("image"));
//                }else{
//                    output_json =ContrastEnhance.contrastEnhance(file.getAbsolutePath());
//                }

                jsonObject = new JSONObject(output_json);

                //生成jpeg图片
                OutputStream output_file_imageQuality = new FileOutputStream(outputPath + "\\" + file.getName());
                output_file_imageQuality.write(Base64Util.decode((String) jsonObject.get("image")));
                output_file_imageQuality.flush();
                output_file_imageQuality.close();
            }
        }
        System.out.println("结束");
//        System.out.println(log);
    }
}
